package be.djdb.utils;
import java.io.BufferedReader;/**
* @author Lieven Roegiers
* @copyright 2007
* @from UpdateCV
*/
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.*;
import java.util.jar.Attributes;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.ResourceBundle;

import javax.xml.transform.stream.StreamSource;

import be.djdb.UnConstruct;

/**
* @author Lieven Roegiers
* @copyright 2007
* @from http://code.google.com/p/javamylibs/
*/
public class AResourceFile {
	private static final long serialVersionUID = 1L;
	private String fileName;
	private String pathName;
	private URL fileUrl ;
	private InputStream is;
	 /**
	 * @param fname
	 * @return the file in a stream
	 * @throws IOException
	 */
	public InputStream takeajarfilestream(String fname) throws IOException{
		 return AResourceFile.class.getResourceAsStream(fname);
	}
	//public ResourceBundle getresourceBundle(String fileName){
	//	return this.getresourceBundle(fileName);
	//}
	/*
	 * F:/Workspace/CV/lib/
	 * */
	public InputStream takealocaltestfilestream(String fname) throws IOException{
		 return new FileInputStream(""+fname);
	}
	/**
	 * 
	 * @param src
	 * @param fileName
	 */
	protected void getSourcestream(StreamSource src,String fileName){
		src = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(fileName));
	}
	public InputStream getInputstream(String fileName) throws IOException{
		URL fileUrl = this.getClass().getClassLoader().getResource(fileName);
		log(Level.WARNING,"GetInputstream of string =>Filename"+ fileName);
		return fileUrl.openConnection().getInputStream();
	}
	public InputStream getInputstream(URL location) throws IOException{
		return location.openConnection().getInputStream();
	}
	public InputStreamReader getInputStreamReader() throws IOException{
		 this.is = fileUrl.openConnection().getInputStream();
		DataInputStream   dis   = new DataInputStream( this.is );
		return new InputStreamReader(dis);
		//BufferedReader br = new BufferedReader( isr );
	}

	public boolean isFile() {
		return (fileUrl != null);
	}
	@UnConstruct
	public void readftpInput() throws IOException{
        //String takNaam;
		//URL url = new URL("http://www.domein.org/3/extra/.txt");
	      //  BufferedReader input = new BufferedReader (new InputStreamReader ((url.openStream())));
	    //..   ....
	}
	@UnConstruct
	public void saveftpOutput() throws IOException{
		// SimpleFTP ftp = new SimpleFTP();
		//ftp.connect("ftp://ftp.domein.org", 21, "***", "***"); //hier gaat het fout, de username en paswoord kloppen zeker en vast
		    //  ....
	}
	public void printInputStream() throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
		String line;
		while (null != (line = br.readLine())) {
		   System.out.println(line);
		}
	}
	public void printInputStream(InputStream in) throws IOException{
		 BufferedInputStream bin = new BufferedInputStream(in);
		 int b;
		 while ( ( b = bin.read() ) != -1 )
		 {
		     char c = (char)b;         
		     System.out.print(""+(char)b); //This prints out content that is unreadable.
		                                   //Isn't it supposed to print out html tag?
		 }
	}
	/*
	 * InputStream testStream= new FileInputStream("F:/Workspace/srcaanloop/test.dat");
	 * OutputStream out = System.out;
	**/
	public void printInputStreamToout(InputStream teststream,OutputStream out) throws IOException{
	    byte buf[]=new byte[1024];
	    int len;
		while((len=teststream.read(buf))>0)
			    out.write(buf,0,len);
			    out.close();
	}
	public void printInputStreamAschar(InputStream iss)throws IOException{
	    int ch;  
			while (((ch) = iss.read()) != -1) {
			         String aChar = new Character((char)ch).toString();
			         System.out.println(aChar);
			  }
	}
	public void printinputstreamAsLine(InputStream iss) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(iss));
		String line = br.readLine();
		while(line!=null){
			System.out.println(line);
			line = br.readLine();
		}
	}
	public void printInputStreamAsChar(InputStream input) throws IOException{
		int data = input.read();
		int charint;
		int i = 0;
		while ((charint = input.read()) != -1 ) {
				i++;
			  data = input.read();
			  System.out.print((char)charint);
		}
		if((charint = input.read()) != -1){
		}else{
			System.out.println("iets mis met de stream");
		}
	}
	public void printOutputStream(OutputStream out) throws IOException{
		System.out.println(out.toString());
	}
    private static  void log(Level level, String msg ){
        String tag ="<>>>>*JAVA_LIBS-�L Roegiers 80072631156*<<<<>Take Resourcefile<>";
        //this.msg += tag + msg + "<br />";
        Logger.getLogger(AResourceFile.class.getName()).log(level, tag + msg);
    }
}
//@stuff
//(FileInputStream)
//jarClassLoader.loadClass()
// URL u = new URL("jar", "", jarfile.toString() );
// JarURLConnection uc = (JarURLConnection)u.openConnection();
// Attributes attr = uc.getMainAttributes();
// System.out.println( attr != null ? attr.getValue( Attributes.Name.MAIN_CLASS) : null);
// return uc.getInputStream();
//return this.getClass().getClassLoader().getResource(jarfile.toString()).openConnection().getInputStream();

//BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("html.htm")));
//String line = br.readLine();
//while(line!=null){
//	output+=line + "\n";
//	line = br.readLine();
//}

